import {createStore} from 'redux'
import {Type,Food} from '../types/food'

interface StateInterface{
    list:Type[]
}


let reducer = (state:StateInterface={list:[]},action:any) => {
    switch (action.type) {
        case 'INIT_DATA':
            //list 
            return {list:[...action.list]}
        case 'CHANGE_HOBIT':{
            //t_id id frequency
            let t_index = state.list.findIndex(item => item.t_id== action.t_id);
            let index:any = state.list[t_index].children.findIndex(v => v.id == action.id);
            let target = state.list[t_index].children[index];
            target.frequency = action.frequency;
            state.list[t_index].children[index] = {...target};
            return {list:[...state.list]}
        }
        default:
            return state;
    }
}

let store = createStore(reducer);

export default store

